﻿
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SiteManager;
using System.Data.SqlClient;

public partial class includes_UcStandByDoctors : System.Web.UI.UserControl
{
    // creating an instance of manager class
    Manager mgr = new Manager(true, "ConnectionString");
    string _fromPage = "";
    bool _isBlank = false;
    bool _isSpecialty = false;

    public bool isSpecialty
    {
        get { return _isSpecialty; }
       
    }
    public string fromPage
    {
        set { _fromPage = value; }
        get { return _fromPage; }
    }
    public bool isBlank
    {
        get { return _isBlank; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        btnSubmit.Attributes.Add("onclick", "return " + this.ClientID + "chkError();");
        if (!Page.IsPostBack)
        {
            // fill drop downs
            mgr.BindList("select Id,Name from tbl_Speciality order by name", "Name", "Id", ddlSpeciality, "select");
            mgr.BindList("select [countryname],countrycode from tbl_country where(countrycode='US')OR (countrycode='CA') order by [countryname]", "countryname", "countrycode", ddlCountry, "select");
            ddlState.Items.Insert(0, new ListItem("Select", "0"));

           
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
       
        string qstring = "";

        if (ddlSpeciality.Items.Count > 1)
        {
            if (ddlSpeciality.SelectedIndex > 0)
            {
                Session["Speciality"] = ddlSpeciality.SelectedItem.Text.Trim().Replace(" ", "_");
                qstring = Session["Speciality"].ToString();
            }
        }



        if (ddlState.Items.Count > 1)
        {
            if (ddlState.SelectedIndex > 0)
            {
                if (qstring == "")
                {
                    qstring = ddlState.SelectedItem.Text.Trim().Replace(" ", "_");
                }
                else
                {
                    qstring = qstring + "/" + ddlState.SelectedItem.Text.Trim().Replace(" ", "_");
                }
            }
        }

        if (txtCity.Text.Trim() != "")
        {
            if (qstring == "")
            {
                qstring = txtCity.Text.Trim().Replace(" ", "_");
            }
            else
            {
                qstring = qstring + "/" + txtCity.Text.Trim().Replace(" ", "_");
            }
        }

        if (txtZipCode.Text.Trim() != "")
        {
            if (qstring == "")
            {
               
                qstring = txtZipCode.Text.Trim().Replace(" ", "_");
            }
            else
            {
               
                qstring = qstring + "/" + txtZipCode.Text.Trim().Replace(" ", "_");
            }
        }

        if (txtLName.Text.Trim().Equals("Last Name:"))
            txtLName.Text = "";



        #region calling stored procedure

        // get the results of the search
        try
        {
            SqlParameter[] sqlParam = new SqlParameter[7];
            sqlParam[6] = new SqlParameter("@CountryId", SqlDbType.VarChar, 2);

            sqlParam[0] = new SqlParameter("@state", SqlDbType.VarChar, 2);
            sqlParam[1] = new SqlParameter("@city", SqlDbType.VarChar, 50);
            sqlParam[2] = new SqlParameter("@zip", SqlDbType.VarChar, 10);
            sqlParam[3] = new SqlParameter("@LastName", SqlDbType.VarChar, 50);
            sqlParam[4] = new SqlParameter("@FirstName", SqlDbType.VarChar, 50);
            sqlParam[5] = new SqlParameter("@SpecialtyID", SqlDbType.Int);
           // sqlParam[6] = new SqlParameter("@hospital", SqlDbType.VarChar, 200);
            //sqlParam[7] = new SqlParameter("@insurance", SqlDbType.VarChar, 200);

            sqlParam[6].Value = ddlCountry.SelectedItem.Value.Equals("0") ? "" : ddlCountry.SelectedItem.Value;
            sqlParam[0].Value = ddlState.SelectedItem.Value.Equals("0") ? "" : ddlState.SelectedItem.Value;
            //(Convert.ToInt32(ddlState.SelectedItem.Value.ToString())>0?ddlState.SelectedItem.Value:"");
            sqlParam[1].Value = txtCity.Text.Trim();
            sqlParam[2].Value = txtZipCode.Text.Trim();
            sqlParam[3].Value = txtLName.Text.Trim();
            //sqlParam[4].Value = txtFName.Text.Trim();
            sqlParam[5].Value = ddlSpeciality.SelectedItem.Value;
            

            //sqlParam[6].Value = tHospitalName;
            if (ddlhospital.Visible == true)
                //sqlParam[6].Value = ddlhospital.SelectedItem.Value;

           

            //sqlParam[7].Value = tInsurance;
            if (ddlInsurance.Visible)
               // sqlParam[7].Value = ddlInsurance.SelectedItem.Value;
            //DataSet ds_result = mgr.ExecuteQuery("advancesearch", sqlParam, "tbl_srch");

            // 8th March
            Session["PreviousSpeciality"] = "";
            Session["PreviousSubSpeciality"] = "";

            if (ddlSpeciality.SelectedIndex > 0)
                Session["PreviousSpeciality"] = ddlSpeciality.SelectedItem.Text;
           
        #endregion
            if (ddlSpeciality.SelectedIndex > 0)
            {

                DataSet ds_result = CustomRepository.CustumDataSetFunction("StandBYDocSearch", sqlParam, "tbl_srch");

                
                string StandByDoctorsIds = null;
                if (ds_result != null) 
                {
                    StandByDoctorsIds = GetStandabyDoctorsId(ds_result); 
                }
                    
                Session["StandByDoctorsId"] = StandByDoctorsIds;

                if (StandByDoctorsIds.Trim().Length > 0)
                {

                    Response.Redirect("discountDoctors.aspx" ,true);
                  
                }
                else
                {
                   
                    _isBlank = true;
                }
            }
            else
            {
                _isSpecialty = true;
               
            }
            
        }
          
        catch (Exception ex)
        {
            //throw;
            Response.Write(ex.ToString());

        }


    }

 private string GetStandabyDoctorsId(DataSet ds)
 {
     string DoctorsIds = "";
     string DocId;
     string SdocId;
     DataCalendarDataContext DCD = new DataCalendarDataContext();
     var query = DCD.CalendarEvents.Select(m => m.doctorID).Distinct();
     try
     {
         if (ds.Tables[0].Rows.Count > 0)
         {

             foreach (int id in query)
             {
                 var liId = (from DataRow row in ds.Tables[0].Rows
                             where ((int)row[0]) == id
                             select row[0]).FirstOrDefault();
                 if (liId != null)
                 {
                     DoctorsIds += liId.ToString() + ",";
                 }
             }
         }

     }
     catch
     {
         DoctorsIds = "";
     }
     if (DoctorsIds.Length > 1)
     { return DoctorsIds.Remove(DoctorsIds.Length - 1); }
     else
     { return DoctorsIds; }
     //try
     //{
        
     //    if (ds.Tables[0].Rows.Count > 0)
     //    {
     //        int index = 0;
     //        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
     //        {
     //                DocId = ds.Tables[0].Rows[i][0].ToString();
                    
     //                foreach (var c in query)
     //                {
     //                   SdocId = c.ToString();
     //                   if (DocId == SdocId)
     //                   {
     //                       if (index != 0)
     //                       {
     //                           DoctorsIds += "," + DocId.ToString();
     //                       }
     //                       else
     //                       {
     //                           DoctorsIds += DocId.ToString();
     //                           index = index + 1;
     //                       }

     //                   }
                       
     //                }
     //        }
     //    }
     //}
     //catch
     //{
     //    DoctorsIds = "";
     //}

     //return DoctorsIds;


 }

    private string GetDoctorsId(DataSet ds)
    {
        string DoctorsIds = "";

        try
        {
          
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (i == 0)
                    {
                        DoctorsIds = ds.Tables[0].Rows[i][0].ToString();
                    }
                    else
                    {
                        DoctorsIds += "," + ds.Tables[0].Rows[i][0].ToString();
                    }
                }
            }
        }
        catch
        {
            DoctorsIds = "";
        }

        return DoctorsIds;


    }

    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        string countryID = ddlCountry.SelectedValue;
       
        mgr.BindList("select stateid,name from tbl_states where CountryID='" + countryID + "'  order by name", "Name", "StateId", ddlState, "--Any--");
        
        if (ddlCountry.SelectedValue == "US")
        {
            ddlState.Items.Insert(0, new ListItem("Select a State", "0"));
        }
        else
        {
            ddlState.Items.Insert(0, new ListItem("Select a Province", "0"));
        }
    }
}
